package cn.medemede.servlet.action;

import cn.medemede.dao.UserpwdDao;
import cn.medemede.dao.UsersDao;
import cn.medemede.model.CharacterBean;
import cn.medemede.model.UserpwdBean;
import cn.medemede.model.UsersBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;

public class Log_check implements Action,Serializable{

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String farward=null;
        PrintWriter out = response.getWriter();
        String username = request.getParameter("username");
        System.out.println("username:" + username);
        String password = request.getParameter("password");
        System.out.println("password:" + password);
        String code = (String) request.getSession().getAttribute("code");
        System.out.println("code:" + code);
        code=code.toLowerCase();

        String icode = request.getParameter("checkcode");
        icode=icode.toLowerCase();
        System.out.println("checkcode:" + icode);

        UserpwdBean userpwdBean = new UserpwdBean();
        userpwdBean.setUsername(username);
        userpwdBean.setPassword(password);

        if (code.equals(icode)){
            System.out.println("验证码正确！");
            if (UserpwdDao.find(userpwdBean)){
                farward = "log_suc";
                HttpSession session=request.getSession();
                UsersBean usersBean=UsersDao.find(username);
                System.out.println("Character:"+usersBean.getCharacter());
                switch (usersBean.getCharacter()){
                    case "admin":{
                        usersBean.setCharacter("管理员");
                        session.setAttribute("user", usersBean);//存储User对象
                        session.setAttribute("character", CharacterBean.getAdmin());
                        //ArrayList<UsersBean> arrayList= UsersDao.find2();
                        //session.setAttribute("stulist",arrayList);//存储学生列表
                        break;
                    }
                    case "teacher":{
                        usersBean.setCharacter("教师");
                        session.setAttribute("user", usersBean);//存储User对象
                        session.setAttribute("character",CharacterBean.getTeacher());
                        //ArrayList<UsersBean> arrayList= UsersDao.find2();
                        //session.setAttribute("stulist",arrayList);//存储学生列表
                        break;
                    }
                    case "student":{
                        usersBean.setCharacter("学生");
                        session.setAttribute("user", usersBean);//存储User对象
                        session.setAttribute("character",CharacterBean.getStudent());
                        if(session.getAttribute("stulist")!=null)
                            session.removeAttribute("stulist");
                        break;
                    }
                    default:{
                        farward="无法获得用户角色！";
                        out.println("errorUserPwd");
                    }
                }
            }else{
                farward="用户名或密码错误！";
                out.println("errorUserPwd");
            }
        }else {
            farward="验证码错误！";
            out.println("errorCode");
        }

        return farward;
    }
}
